#include "TemperatureCtrl.h"
#include "MCUConfig.h"
#include "KeyEventConvetor.h"

#if (1 == R100B3950PD2001)
float CentigradeToADC(float CentiGrade)
{
    float x = CentiGrade;
    if (CentiGrade < 22.0)
    {
        return (0.00000001 * x * x * x * x * x + 0.00000234 * x * x * x * x + 0.00034121 * x * x * x + 0.02793643 * x * x + 1.26584838 * x + 24.89827955);
    }
    else if (CentiGrade < 120.0)
    {
        return (-0.0000000478 * x * x * x * x * x + 0.0000098661 * x * x * x * x - 0.0000849666 * x * x * x + 0.0384023656 * x * x + 1.1934743518 * x + 24.4047328746);
    }
    else
    {
        return (-0.0000000283 * x * x * x * x * x + 0.0000309431 * x * x * x * x - 0.0131507734 * x * x * x + 2.6216430053 * x * x - 220.3416396579 * x + 7132.1762873281);
    }
}

float ADCToCentigrade(float ADCValue)
{
    float x = ADCValue / 1000.0;
    if (x < 0.070486)
    {
        return (440531544.62649300 * x * x * x * x * x - 87247251.14961710 * x * x * x * x + 6605044.88011378 * x * x * x - 244494.51575123 * x * x + 5, 152.81277881 * x - 49.64269429);
    }
    else if (x < 1.130467)
    {
        return (112.48813642 * x * x * x * x * x - 477.71189256 * x * x * x * x + 786.15708653 * x * x * x - 643.75692559 * x * x + 321.87106322 * x + 3.4984);
    }
    else
    {
        return (4.32603540 * x * x * x * x * x - 49.47682845 * x * x * x * x + 227.31952038 * x * x * x - 517.62202627 * x * x + 618.47527333 * x - 189.94073226);
    }
}

//����ʵ���ҽ��е��¶Ȳ���
float CentigradeCompensate(short Temp)
{
    float x = Temp;
    if (x > 25)
    {
        return (0.8908 * x + 1.330);
    }
    else
    {
        return x;
    }
}

//����ʵ���ҽ��е��¶Ȳ���,����ֵ��Ҫʵ�ʲ���
float CentigradeCompensateAb(short Temp)
{
    float x = Temp;
    if (x > 25)
    {
        return (0.849167 * x + 4.66667);
    }
    else
    {
        return x;
    }
}

#endif

#define TIME_GAP 4
#define NUM_TEMP 3

#if 1
xdata short TempRecord[NUM_TEMP] = {12, 12, 12};
void UpdateTemp(short currentTemp)
{
    TempRecord[NUM_TEMP - 1] = TempRecord[NUM_TEMP - 2];
    TempRecord[NUM_TEMP - 2] = TempRecord[NUM_TEMP - 3];
    TempRecord[NUM_TEMP - 3] = currentTemp;
}

void ClearTemp(short Temp)
{
    TempRecord[NUM_TEMP - 3] = TempRecord[NUM_TEMP - 2] = TempRecord[NUM_TEMP - 1] = Temp;
}

//2s To Ctrl
xdata short TempChange = 0;
bool PIDCtrlTemp(short dstTemp, short currentTemp)
{
    UpdateTemp(currentTemp);
    TempChange = 0;
    if (TempRecord[0] > TempRecord[2])
    {
        TempChange = TempRecord[0] - TempRecord[2];
        if ((currentTemp + TIME_GAP * (TempChange)) > dstTemp)
        {
            return false;
        }
        else
        {
            if (currentTemp < dstTemp)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    else
    {
        TempChange = TempRecord[2] - TempRecord[0];
        if (currentTemp < (dstTemp + TIME_GAP * (TempChange)))
        {
            return true;
        }
        else
        {
            if (currentTemp > dstTemp)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

#endif



xdata short TempRecord1[NUM_TEMP] = {12, 12, 12};
void UpdateTemp1(short currentTemp)
{
    TempRecord1[NUM_TEMP - 1] = TempRecord1[NUM_TEMP - 2];
    TempRecord1[NUM_TEMP - 2] = TempRecord1[NUM_TEMP - 3];
    TempRecord1[NUM_TEMP - 3] = currentTemp;
}

void ClearTemp(short Temp)
{
    TempRecord1[NUM_TEMP - 3] = TempRecord1[NUM_TEMP - 2] = TempRecord1[NUM_TEMP - 1] = Temp;
}

//2s To Ctrl
xdata short TempChange1 = 0;
bool PIDCtrlTemp(short dstTemp, short currentTemp)
{
    UpdateTemp(currentTemp);
    TempChange1 = 0;
    if (TempRecord1[0] > TempRecord1[2])
    {
        TempChange1 = TempRecord1[0] - TempRecord1[2];
        if ((currentTemp + TIME_GAP * (TempChange1)) > dstTemp)
        {
            return false;
        }
        else
        {
            if (currentTemp < dstTemp)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    else
    {
        TempChange1 = TempRecord1[2] - TempRecord1[0];
        if (currentTemp < (dstTemp + TIME_GAP * (TempChange1)))
        {
            return true;
        }
        else
        {
            if (currentTemp > dstTemp)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}








